import router from './index'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 路由守卫
const whiteList = [] // 不重定向白名单
router.beforeEach((to, from, next) => {
  NProgress.start()
  const USER_TOKEN = window.sessionStorage.getItem('user_token')
  // console.log('进入路由守卫！')
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // console.log('进入守卫验证！')
    if (USER_TOKEN !== 'admin') {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      // console.log('通过守卫进入 from 路由！')
      next() // 确保一定要调用 next()
      NProgress.done()
    }
  }
})
router.afterEach(() => {
  NProgress.done() // 结束Progress
})
